package prettyprolog.engine.functions;

import prettyprolog.datatypes.Real;
import prettyprolog.datatypes.Term;
import prettyprolog.engine.Function;
import prettyprolog.engine.FunctionException;

public class DividedBy extends Function {

	private static final DividedBy inst = new DividedBy();
	
	public static DividedBy getInstance() {
		return inst;
	}
	
	@Override
	public Term apply(Term... args) throws FunctionException {
		double result = 1;
		for(int i = 0; i < args.length; i++) {
			if(i == 0) {
				result = ((Real) args[0]).getValue();
			} else {
				try {
					result /= ((Real) args[i]).getValue();
				} catch(ClassCastException e) {
					throw new FunctionException("Not a number: " + args[i], e);
				}
			}
		}
		return new Real(result);
	}

}
